1   /*
2    * Copyright (C) 2007 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect;
18  
19  import com.google.common.annotations.GwtCompatible;
20  
21  import java.util.Map;
22  import java.util.Set;
23  
24  import javax.annotation.Nullable;
25  
26  /**
27   * A bimap (or "bidirectional map") is a map that preserves the uniqueness of
28   * its values as well as that of its keys. This constraint enables bimaps to
29   * support an "inverse view", which is another bimap containing the same entries
30   * as this bimap but with reversed keys and values.
31   * 
32   * <p>See the Guava User Guide article on <a href=
33   * "http://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained#BiMap">
34   * {@code BiMap}</a>.
35   *
36   * @author Kevin Bourrillion
37   * @since 2.0 (imported from Google Collections Library)
38   */
39  @GwtCompatible
40  public interface BiMap<K, V> extends Map<K, V> {
41    // Modification Operations
42  
43    /**
44     * {@inheritDoc}
45     *
46     * @throws IllegalArgumentException if the given value is already bound to a
47     *     different key in this bimap. The bimap will remain unmodified in this
48     *     event. To avoid this exception, call {@link #forcePut} instead.
49     */
50    @Override
51    V put(@Nullable K key, @Nullable V value);
52  
53    /**
54     * An alternate form of {@code put} that silently removes any existing entry
55     * with the value {@code value} before proceeding with the {@link #put}
56     * operation. If the bimap previously contained the provided key-value
57     * mapping, this method has no effect.
58     *
59     * <p>Note that a successful call to this method could cause the size of the
60     * bimap to increase by one, stay the same, or even decrease by one.
61     *
62     * <p><b>Warning:</b> If an existing entry with this value is removed, the key
63     * for that entry is discarded and not returned.
64     *
65     * @param key the key with which the specified value is to be associated
66     * @param value the value to be associated with the specified key
67     * @return the value which was previously associated with the key, which may
68     *     be {@code null}, or {@code null} if there was no previous entry
69     */
70    V forcePut(@Nullable K key, @Nullable V value);
71  
72    // Bulk Operations
73  
74    /**
75     * {@inheritDoc}
76     *
77     * <p><b>Warning:</b> the results of calling this method may vary depending on
78     * the iteration order of {@code map}.
79     *
80     * @throws IllegalArgumentException if an attempt to {@code put} any
81     *     entry fails. Note that some map entries may have been added to the
82     *     bimap before the exception was thrown.
83     */
84    @Override
85    void putAll(Map<? extends K, ? extends V> map);
86  
87    // Views
88  
89    /**
90     * {@inheritDoc}
91     *
92     * <p>Because a bimap has unique values, this method returns a {@link Set},
93     * instead of the {@link java.util.Collection} specified in the {@link Map}
94     * interface.
95     */
96    @Override
97    Set<V> values();
98  
99    /**
100    * Returns the inverse view of this bimap, which maps each of this bimap's
101    * values to its associated key. The two bimaps are backed by the same data;
102    * any changes to one will appear in the other.
103    *
104    * <p><b>Note:</b>There is no guaranteed correspondence between the iteration
105    * order of a bimap and that of its inverse.
106    *
107    * @return the inverse view of this bimap
108    */
109   BiMap<V, K> inverse();
110 }